home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP05.ZIP / CHAP05 / COSCHMOO / DOCUMENT.CPP < prev    next >
C/C++ Source or Header  |  1993-05-02  |  19KB  |  944 lines

  1. /*
  2.  * DOCUMENT.CPP
  3.  * Component Schmoo Chapter 5
  4.  *
  5.  * Implementation of the CSchmooDoc derivation of CDocument as
  6.  * well as an implementation of CPolylineAdviseSink.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "coschmoo.h"
  19.  
  20.  
  21.  
  22. /*
  23.  * CSchmooDoc::CSchmooDoc
  24.  * CSchmooDoc::~CSchmooDoc
  25.  *
  26.  * Constructor Parameters:
  27.  *  hInst           HINSTANCE of the application.
  28.  */
  29.  
  30. CSchmooDoc::CSchmooDoc(HINSTANCE hInst)
  31.     : CDocument(hInst)
  32.     {
  33.     m_uPrevSize=SIZE_RESTORED;
  34.     m_pPL=NULL;
  35.     m_pPLAdv=NULL;
  36.  
  37.     //CHAPTER5MOD
  38.     m_pIPersistStorage=NULL;
  39.     //End CHAPTER5MOD
  40.     return;
  41.     }
  42.  
  43.  
  44. CSchmooDoc::~CSchmooDoc(void)
  45.     {
  46.     //The client takes care of destroying document windows.
  47.  
  48.     if (NULL!=m_pPLAdv)
  49.         m_pPLAdv->Release;
  50.  
  51.     //CHAPTER5MOD
  52.     if (NULL!=m_pIPersistStorage)
  53.         m_pIPersistStorage->Release();
  54.     //End CHAPTER5MOD
  55.  
  56.     if (NULL!=m_pPL)
  57.         m_pPL->Release();
  58.  
  59.     CoFreeUnusedLibraries();
  60.     return;
  61.     }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. /*
  69.  * CSchmooDoc::FInit
  70.  *
  71.  * Purpose:
  72.  *  Initializes an already created document window.  The client actually
  73.  *  creates the window for us, then passes that here for further
  74.  *  initialization.
  75.  *
  76.  * Parameters:
  77.  *  pDI             LPDOCUMENTINIT containing initialization parameters.
  78.  *
  79.  * Return Value:
  80.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  81.  */
  82.  
  83. BOOL CSchmooDoc::FInit(LPDOCUMENTINIT pDI)
  84.     {
  85.     RECT        rc;
  86.     HRESULT     hr;
  87.  
  88.     //Change the stringtable range to our customization.
  89.     pDI->idsMin=IDS_DOCUMENTMIN;
  90.     pDI->idsMax=IDS_DOCUMENTMAX;
  91.  
  92.     //Do default initialization
  93.     if (!CDocument::FInit(pDI))
  94.         return FALSE;
  95.  
  96.     //Create the Polyline Object via COMPOBJ.DLL functions.
  97.     //CHAPTER5MOD
  98.     hr=CoCreateInstance(CLSID_Polyline5, NULL, CLSCTX_INPROC_SERVER
  99.         , IID_IPolyline5, (LPVOID FAR *)&m_pPL);
  100.     //End CHAPTER5MOD
  101.  
  102.     if (FAILED(hr))
  103.         return FALSE;
  104.  
  105.     //Initialize the contained Polyline which creates a window.
  106.     GetClientRect(m_hWnd, &rc);
  107.     InflateRect(&rc, -8, -8);
  108.  
  109.     if (FAILED(m_pPL->Init(m_hWnd, &rc, WS_CHILD | WS_VISIBLE, ID_POLYLINE)))
  110.         return FALSE;
  111.  
  112.     //Set up an advise on the Polyline.
  113.     m_pPLAdv=new CPolylineAdviseSink((LPVOID)this);
  114.     m_pPL->SetAdvise(m_pPLAdv);
  115.  
  116.     //CHAPTER5MOD
  117.     //Get the IPersistStorage interface on the object for loads & saves.
  118.     hr=m_pPL->QueryInterface(IID_IPersistStorage, (LPVOID FAR *)&m_pIPersistStorage);
  119.  
  120.     if (FAILED(hr))
  121.         return FALSE;
  122.     //End CHAPTER5MOD
  123.  
  124.     return TRUE;
  125.     }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. /*
  134.  * CSchmooDoc::FMessageHook
  135.  *
  136.  * Purpose:
  137.  *  Processes WM_SIZE for the document so we can resize the Polyline.
  138.  *
  139.  * Parameters:
  140.  *  <WndProc Parameters>
  141.  *  pLRes           LRESULT FAR * in which to store the return value
  142.  *                  for the message.
  143.  *
  144.  * Return Value:
  145.  *  BOOL            TRUE to prevent further processing, FALSE otherwise.
  146.  */
  147.  
  148. BOOL CSchmooDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  149.     , LPARAM lParam, LRESULT FAR *pLRes)
  150.     {
  151.     UINT        dx, dy;
  152.     RECT        rc;
  153.  
  154.     if (WM_SIZE==iMsg)
  155.         {
  156.         //Don't effect the Polyline size to or from minimized state.
  157.         if (SIZE_MINIMIZED!=wParam && SIZE_MINIMIZED !=m_uPrevSize)
  158.             {
  159.             //When we change size, resize any Polyline we hold.
  160.             dx=LOWORD(lParam);
  161.             dy=HIWORD(lParam);
  162.  
  163.             /*
  164.              * If we are getting WM_SIZE in response to a Polyline
  165.              * notification, then don't resize the Polyline window again.
  166.              */
  167.             if (!m_fNoSize && NULL!=m_pPL)
  168.                 {
  169.                 //Resize the polyline to fit the new client
  170.                 SetRect(&rc, 8, 8, dx-8, dy-8);
  171.                 m_pPL->RectSet(&rc, FALSE);
  172.  
  173.                 /*
  174.                  * We consider sizing something that makes the file dirty,
  175.                  * but not until we've finished the create process, which
  176.                  * is why we set fNoDirty to FALSE in WM_CREATE since we
  177.                  * get a WM_SIZE on the first creation.
  178.                  */
  179.                 if (!m_fNoDirty)
  180.                     FDirtySet(TRUE);
  181.  
  182.                 SetRect(&rc, 0, 0, dx, dy);
  183.  
  184.                 if (NULL!=m_pAdv)
  185.                     m_pAdv->OnSizeChange((LPCDocument)this, &rc);
  186.  
  187.                 m_fNoDirty=FALSE;
  188.                 }
  189.             }
  190.  
  191.         m_uPrevSize=wParam;
  192.         }
  193.  
  194.     /*
  195.      * We return FALSE even on WM_SIZE so we can let the default procedure
  196.      * handle maximized MDI child windows appropriately.
  197.      */
  198.     return FALSE;
  199.     }
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. /*
  209.  * CSchmooDoc::Clear
  210.  *
  211.  * Purpose:
  212.  *  Sets all contents in the document back to defaults with no filename.
  213.  *
  214.  * Paramters:
  215.  *  None
  216.  *
  217.  * Return Value:
  218.  *  None
  219.  */
  220.  
  221. void CSchmooDoc::Clear(void)
  222.     {
  223.     //Completely reset the polyline
  224.     m_pPL->New();
  225.  
  226.     CDocument::Clear();
  227.     return;
  228.     }
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235. /*
  236.  * CSchmooDoc::ULoad
  237.  *
  238.  * Purpose:
  239.  *  Loads a given document without any user interface overwriting the
  240.  *  previous contents of the Polyline window.  We do this by opening
  241.  *  the file and telling the Polyline to load itself from that file.
  242.  *
  243.  * Parameters:
  244.  *  fChangeFile     BOOL indicating if we're to update the window title
  245.  *                  and the filename from using this file.
  246.  *  pszFile         LPSTR to the filename to load, NULL for untitled.
  247.  *
  248.  * Return Value:
  249.  *  UINT            An error value from DOCERR_*
  250.  */
  251.  
  252. UINT CSchmooDoc::ULoad(BOOL fChangeFile, LPSTR pszFile)
  253.     {
  254.     HRESULT             hr;
  255.     //CHAPTER5MOD
  256.     LPSTORAGE           pIStorage;
  257.  
  258.     if (NULL==pszFile)
  259.         {
  260.         //CHAPTER5MOD
  261.         /*
  262.          * As a user of an IPersistStorage we have to provide all objects
  263.          * with an IStorage they can use for incremental access passing
  264.          * that storage to ::InitNew.  Here we create a temporary file
  265.          * that we don't bother holding on to.  If the object doesn't
  266.          * use it, then our ::Release destroys it immediately.
  267.          */
  268.  
  269.         hr=StgCreateDocfile(NULL, STGM_DIRECT | STGM_READWRITE | STGM_CREATE
  270.             | STGM_DELETEONRELEASE | STGM_SHARE_EXCLUSIVE, 0, &pIStorage);
  271.  
  272.         if (FAILED(hr))
  273.             return DOCERR_COULDNOTOPEN;
  274.  
  275.         m_pIPersistStorage->InitNew(pIStorage);
  276.         pIStorage->Release();
  277.         //End CHAPTER5MOD
  278.  
  279.         Rename(NULL);
  280.         return DOCERR_NONE;
  281.         }
  282.  
  283.     //CHAPTER5MOD
  284.     /*
  285.      * Open a storage and pass it to the Polyline via IPersistStorage.
  286.      * We do not remain compatible with previous files saved with
  287.      * Component Schmoo.
  288.      */
  289.  
  290.     hr=StgOpenStorage(pszFile, NULL, STGM_DIRECT | STGM_READ
  291.         | STGM_SHARE_EXCLUSIVE, NULL, 0, &pIStorage);
  292.  
  293.     if (FAILED(hr))
  294.         return DOCERR_COULDNOTOPEN;
  295.  
  296.     hr=m_pIPersistStorage->Load(pIStorage);
  297.  
  298.     pIStorage->Release();
  299.     //End CHAPTER5MOD
  300.  
  301.     if (FAILED(hr))
  302.         return DOCERR_READFAILURE;
  303.  
  304.     if (fChangeFile)
  305.         Rename(pszFile);
  306.  
  307.     //Importing a file makes things dirty
  308.     FDirtySet(!fChangeFile);
  309.  
  310.     return DOCERR_NONE;
  311.     }
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319. /*
  320.  * CSchmooDoc::USave
  321.  *
  322.  * Purpose:
  323.  *  Writes the file to a known filename, requiring that the user has
  324.  *  previously used FileOpen or FileSaveAs in order to have a filename.
  325.  *
  326.  * Parameters:
  327.  *  uType           UINT indicating the type of file the user requested
  328.  *                  to save in the File Save As dialog.
  329.  *  pszFile         LPSTR under which to save.  If NULL, use the current name.
  330.  *
  331.  * Return Value:
  332.  *  UINT            An error value from DOCERR_*
  333.  */
  334.  
  335. UINT CSchmooDoc::USave(UINT uType, LPSTR pszFile)
  336.     {
  337.     BOOL                fRename=TRUE;
  338.     HRESULT